package com.nuoniu.sibanyun.controller.po;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.bo.*;
import com.nuoniu.sibanyun.entity.po.VPurchaseArrivalDetail;
import com.nuoniu.sibanyun.entity.po.VPurchaseInvoiceDetail;
import com.nuoniu.sibanyun.entity.po.VPurchaseInvoiceMain;
import com.nuoniu.sibanyun.service.erp.IErpVoucherDesignService;
import com.nuoniu.sibanyun.service.po.IVPurchaseInvoiceDetailService;
import com.nuoniu.sibanyun.service.po.IVPurchaseInvoiceMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 采购发票管理
 * @author 小啊刚
 * @since 2020-11-19
 */
@RestController
@RequestMapping("/purchase/purchaseInvoice")
@Api(value="采购发票单管理", tags={"采购发票单管理"})
public class VPurchaseInvoiceMainController extends BaseController {

    @Autowired
    private IVPurchaseInvoiceMainService invoiceMainService;
    @Autowired
    private IVPurchaseInvoiceDetailService invoiceDetailService;
    @Autowired
    private IErpVoucherDesignService erpVoucherDesignService;

    @PostMapping("save")
    @ApiOperation("保存采购发票")
    public HttpResult save(@RequestBody VPurchaseInvoiceMain purchaseInvoiceMain) {
        if(purchaseInvoiceMain.getOrderDetailList() == null || purchaseInvoiceMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }

        invoiceMainService.saveOrder(purchaseInvoiceMain);
        return HttpResult.ok();
    }

    @DeleteMapping("delete")
    @ApiOperation("删除采购发票")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public HttpResult delete(Long id) {
       invoiceMainService.delete(id);
        return HttpResult.ok();
    }

    /**
     * 提交
     */
    @GetMapping("submit")
    @ApiOperation("单据提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:提交,-1:反提交")
    })
    public HttpResult submit(Long id, Integer status) {
        invoiceMainService.submit(id, status);
        return HttpResult.ok();
    }

    @GetMapping("examine")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        invoiceMainService.examine(id,status);
        return HttpResult.ok();
    }

    /**
     * 获取单据编码
     * @author 小啊刚
     * @date 2020-11-27
     */
    @GetMapping("getCode")
    @ApiOperation("获取单据编码")
    public HttpResult getCode() {
        return HttpResult.ok(invoiceMainService.getCode(UserThreadLocal.get().getCompanyId()));
    }


    /**
     * 获取 委外发票 单据编码
     */
    @GetMapping("getCodeForDelegate")
    @ApiOperation("获取 委外发票 单据编码")
    public HttpResult getCodeForDelegate() {
        return HttpResult.ok(invoiceMainService.getCodeForDelegate(UserThreadLocal.get().getCompanyId()));
    }
    /**
     * 保存 委外发票 单据
     */
    @PostMapping("saveOrderForDelegate")
    @ApiOperation("保存 委外发票 单据")
    public HttpResult saveOrderForDelegate(@RequestBody VPurchaseInvoiceMain purchaseInvoiceMain) {
        invoiceMainService.saveOrderForDelegate(purchaseInvoiceMain);
        return HttpResult.ok();
    }

}
